package com.lzk.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzk.auth.service.FactoryService;
import com.lzk.auth.service.TypeService;
import com.lzk.common.result.Result;
import com.lzk.common.utils.MD5;
import com.lzk.model.system.SysFactory;

import com.lzk.model.system.SysType;
import com.lzk.model.system.SysUser;
import com.lzk.vo.system.FactoryVo;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "工厂管理")
@RestController
@RequestMapping("/admin/personal/factory")
public class FactoryController {
    @Autowired
    private FactoryService factoryService;

    @Autowired
    private TypeService typeService;

    @ApiOperation("工厂条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        FactoryVo factoryVo) {
        Page<SysFactory> pageParam = new Page<>(page,limit);

        LambdaQueryWrapper<SysFactory> wrapper = new LambdaQueryWrapper<>();
        Integer typeVo = factoryVo.getFType();
        if(null!=typeVo){
            wrapper.eq(SysFactory::getFType,typeVo);
            System.out.println(typeVo);
        }
        String fNameVo = factoryVo.getFname();
        if(!StringUtils.isEmpty(fNameVo)){
            wrapper.like(SysFactory::getFName,fNameVo);
            System.out.println(fNameVo);
        }

        IPage<SysFactory> pageModel = factoryService.page(pageParam, wrapper);
        List<SysFactory> records = pageModel.getRecords();
        for (SysFactory record : records) {
            Integer fType = record.getFType();
            SysType type = typeService.getById(fType);
            record.setRoleName(type.getName());

        }

        return Result.ok(pageModel);
    }

    @ApiOperation(value = "根据id查工厂")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysFactory factory = factoryService.getById(id);
        return Result.ok(factory);
    }

    @ApiOperation(value = "录入工厂信息")
    @PostMapping("save")
    public Result save(@RequestBody SysFactory factory) {

        factoryService.save(factory);
        return Result.ok();
    }

    @ApiOperation(value = "更新工厂信息")
    @PutMapping("update")
    public Result updateById(@RequestBody SysFactory factory) {
        factoryService.updateById(factory);
        return Result.ok();
    }

    @ApiOperation(value = "删除工厂")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        UpdateWrapper<SysFactory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id); // 根据ID作为更新条件

        updateWrapper.set("is_deleted", 1);
        factoryService.update(updateWrapper);

        return Result.ok();
    }


}
